package study.datastructure.link;/**
 * @program: datastructure
 * @author: lcy
 * @create: 2024-09-20 20:34
 */

import com.sun.javafx.binding.StringFormatter;
import jdk.nashorn.internal.ir.annotations.Reference;

import java.awt.dnd.InvalidDnDOperationException;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.function.Consumer;

/**
 2024/9/20,
 */
//单向链表
public class SingleLinkList{
      static Node head=null; //头指针

    /**
     * 向头部添加元素
     */
    public  void addFirst(int value) {
        //链表为空
     /*   head=new Node(val,null);*/

        //链表不为空
        head=new Node(value,head);
    }

    /**
     * 遍历链表
     */
    public  void loop(Consumer<Integer> consumer){
        Node pointer=head;
        while (pointer!=null) {
            consumer.accept(pointer.value);
            pointer=pointer.next;
        }
    }

    /**
     * 递归遍历
     * @param before
     * @param after
     */
    public void loop1(Consumer<Integer> before,Consumer<Integer> after){
        recursion(before,head,after);
    }
    private void recursion(Consumer<Integer> before,Node curr,Consumer<Integer> after){
        if (curr==null) {
            return;
        }
        before.accept(curr.value);
        recursion(before,curr.next,after);
        after.accept(curr.value);
    }
    /**
     * 节点内部类
     */
     public  static class Node{ //内部类 节点
         int value;
         Node next=null;

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }

    /**
     * 添加最后节点
     */
    public void addLast(Integer value) {
        Node lastNode=findLastNode();
        if (lastNode==null){
            addFirst(value);
            return;
        }
        lastNode.next=new Node(value,null);

    }

    /**
     * 找到最后一个节点
     * @return
     */

    private Node findLastNode() {
        if (head==null){
            return null;
        }
        Node p; //尾节点的判断细节
        for (p=head;p.next!=null;p=p.next){
        }

        return p;
    }

    public Integer findNodeValueByIndex(Integer index){
        Node indexNode=findNode(index);
        if (indexNode==null){
            illegalIndex(index);
        }
        return indexNode.value;

    }

    private Node findNode(Integer index) {
        if (index==null||index<0){
            return null;
        }else if (index==0){
            return head;
        }
        Node p;
        int i=0;//索引
        for(p=head;p!=null;p=p.next,i++){
            if (index==i){
                return p;
            }
        }
        return null;
    }
    public void insert(int index, int value) {
        if (index==0) { //插入的是头元素
            addFirst(index);
            return;
        }
        //超过索引范围
        Node previousNode = findNode(index - 1);
        if (previousNode==null){
                illegalIndex(index);
                return;
        }
        previousNode.next=new Node(value,previousNode.next);
    }

    /**
     * 移除头元素
     */
    public void removeFirst(){
        if (head==null){
            illegalIndex(0);
            return;
        }
        head=head.next; //gc回收
    }

    public void remove(int index){
        if (index==0) {
            removeFirst();
            return;
        }
        Node previousNode = findNode(index-1);
        if (previousNode==null){
            illegalIndex(index);
            return;
        }
        Node removed=previousNode.next;
        if (removed==null){
            illegalIndex(index);
            return;
        }
        previousNode.next=removed.next;

    }


    private static void illegalIndex(int index) {
        throw new IllegalArgumentException(String
                .format("参数index [%d],不合法%n", index));
    }


}
